<?php
// +----------------------------------------------------------------------
// | 小牛Admin
// +----------------------------------------------------------------------
// | Website: www.xnadmin.cn
// +----------------------------------------------------------------------
// | Author: dav <85168163@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\ArticleClassModel;
use app\common\model\ArticleModel;
use think\facade\View;
use utils\Data;

class Article extends AdminBase
{
    /**
     * 文章列表
     */
    public function index()
    {
        if ( $this->request->isAjax() ) {
            $param = $this->request->param();
            $model = new ArticleModel();
            //构造搜索条件
            if( $param['cid']!='' ) {
                $model = $model->where('cid',$param['cid']);
            }
            if( $param['title']!='' ) {
                $model = $model->whereLike('title',"%".$param['title']."%");
            }
            if( $param['start_date']!=''&&$param['end_date']!='' ) {
                $start_date = $param['start_date'].' 00:00:00';
                $end_date = $param['end_date'].' 23:59:59';
                $model = $model->whereBetweenTime('create_time',$start_date,$end_date);
            }
            $list = $model->with(['class'])
                ->order('id desc')
                ->paginate([
                    'query' => $param,
                    'list_rows'=>$this->request->param('limit',20)
                ]);
            $this->success('获取成功', '', $list);
        }

        //分类数据
        $this->assignTree();
        return view();
    }

    /**
     * 添加文章
     */
    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = ArticleModel::create($param);
            if( $result ) {
                xn_add_admin_log('添加文章');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }

        //分类数据
        $this->assignTree();
        return view('form');
    }

    /**
     * 编辑文章
     */
    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = ArticleModel::update($param);
            if( $result ) {
                xn_add_admin_log('编辑文章');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->get('id');
        $data = ArticleModel::where('id',$id)->find();
        $cid = $data['cid'];

        //分类数据
        $this->assignTree();
        return view('form',compact('data','cid'));
    }

    /**
     * 删除文章
     */
    public function delete()
    {
        $id = $this->request->param('id');
        ArticleModel::destroy($id);
        xn_add_admin_log('删除文章');
        $this->success('删除成功');
    }

    /**
     * 分类树
     */
    private function assignTree()
    {
        $list = ArticleClassModel::order('sort asc, id asc')->select()->toArray();
        $list = Data::tree($list, 'name','id');
        View::assign('class_list',$list);
    }
}